package cn.yuan.xiaoyu.chatmodule.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.chatmodule.adapter.ChartListVIewAdapter;
import cn.yuan.xiaoyu.chatmodule.bean.SendMessageBean;
import cn.yuan.xiaoyu.chatmodule.presenter.ChatPresenter;
import cn.yuan.xiaoyu.chatmodule.presenter.IChatView;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;

/**
 * Created by yukuoyuan on 2017/2/25.
 * 这是一个聊天的界面
 */

public class ChatActivity extends BaseActivity implements IChatView, TextWatcher {
    @Bind(R.id.rcv_chat)
    RecyclerView rcvChat;
    @Bind(R.id.et_chart_inputxttmsg)
    EditText etChartInputxttmsg;
    @Bind(R.id.iv_send_face)
    ImageView ivSendFace;
    @Bind(R.id.bt_send_msg)
    Button btSendMsg;
    @Bind(R.id.iv_more_sendmsg)
    ImageView ivMoreSendmsg;
    @Bind(R.id.gv_send_emojmsg)
    GridView gvSendEmojmsg;
    @Bind(R.id.tv_send_photomsg)
    TextView tvSendPhotomsg;
    @Bind(R.id.iv_toolbar_left)
    ImageView ivToolbarLeft;
    @Bind(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    private ChatPresenter chatPresenter;
    public List<SendMessageBean> list = new ArrayList<>();
    private ChartListVIewAdapter chartListVIewAdapter;

    @Override
    protected void bindPresenter() {
        chatPresenter = new ChatPresenter(this, this);
    }

    @Override
    protected void initData(Bundle extras) {
        chartListVIewAdapter = new ChartListVIewAdapter(list);
        rcvChat.setLayoutManager(new LinearLayoutManager(this));
        rcvChat.setAdapter(chartListVIewAdapter);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_chat;
    }

    @Override
    protected void initToolbar() {
        etChartInputxttmsg.addTextChangedListener(this);
        ivToolbarLeft.setVisibility(View.VISIBLE);
        tvToolbarTitle.setVisibility(View.VISIBLE);
        tvToolbarTitle.setText("小🐟");
    }

    @OnClick({R.id.iv_send_face, R.id.bt_send_msg, R.id.iv_more_sendmsg, R.id.tv_send_photomsg, R.id.iv_toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_send_face:
                break;
            case R.id.bt_send_msg:
                chatPresenter.sendMessage(getMsg());
                break;
            case R.id.iv_more_sendmsg:
                break;
            case R.id.tv_send_photomsg:
                break;
            case R.id.iv_toolbar_left:
                onBackPressed();
                break;
        }
    }

    @Override
    public String getMsg() {
        return etChartInputxttmsg.getText().toString().trim();
    }

    @Override
    public void clearEditText() {
        etChartInputxttmsg.setText("");
    }

    @Override
    public void addMsg2List(SendMessageBean sendMessageBean) {
        chartListVIewAdapter.addData(sendMessageBean);
        rcvChat.smoothScrollToPosition(list.size());

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(getMsg())) {
            btSendMsg.setVisibility(View.GONE);
            ivMoreSendmsg.setVisibility(View.VISIBLE);
        } else {
            btSendMsg.setVisibility(View.VISIBLE);
            ivMoreSendmsg.setVisibility(View.GONE);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

}
